﻿using OrderSystem.DataAccess;
using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormAddOperator : Form
    {
        public FormAddOperator()
        {
            this.InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.txtOperatorName.Text.Trim() == "" || this.txtOperatorName.Text.Trim().Length > 32)
            {
                MessageBox.Show("姓名不能为空或者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtOperatorName.Focus();
            }
            else if (this.txtPhone.Text.Trim() == "" || this.txtPhone.Text.Trim().Length > 64)
            {
                MessageBox.Show("电话不能为空者不能大于64个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPhone.Focus();
            }
            else if (this.txtAddress.Text.Trim() == "" || this.txtAddress.Text.Trim().Length > 128)
            {
                MessageBox.Show("地址不能为空者不能大于128个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtAddress.Focus();
            }
            else if (this.txtLoginName.Text.Trim() == "" || this.txtLoginName.Text.Trim().Length > 32)
            {
                MessageBox.Show("登陆名不能为空者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLoginName.Focus();
            }
            else if (this.txtPassword.Text == "" || this.txtPassword.Text.Length > 32)
            {
                MessageBox.Show("密码不能为空者不能大于32个字符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtPassword.Focus();
            }
            else
            {
                Operators operators = new Operators();
                bool flag = false;
                try
                {
                    flag = operators.InsertOperator(this.txtOperatorName.Text.Trim(), this.rbMale.Checked, this.txtPhone.Text.Trim(), this.txtAddress.Text.Trim(), this.txtEmail.Text.Trim(), this.txtLoginName.Text.Trim(), this.txtPassword.Text, 6, this.txtRemark.Text.Trim());
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number != 2627)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        MessageBox.Show("此登录名已经存在，不能添加相同的登录名！\r\n\r\n你可以继续使用已经存在的这个登录名 或者 请更换一个登录名在试一下", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                operators.Dispose();
                if (flag)
                {
                    MessageBox.Show("录入成功", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
